<?php defined("MAIN_DIR") or die("No direct access");

$deposit_id = UV::readInt($route->getPath(3));
if ($deposit_id == "") {
	to("/$c_name/deposits/");
}

$deposit_res = mysql_query("select `D`.*,`B`.`city_id`,`B`.`link`,`B`.`name` as `bank_name` from `deposit` as `D`,`bank` as `B` where `D`.`deposit_id` = '$deposit_id' and `D`.`bank_id` = `B`.`bank_id` limit 1");

if (mysql_num_rows($deposit_res) == 0) {
	to("/$c_name/deposits/");
}
$deposit_row = mysql_fetch_array($deposit_res);
$city_row = mysql_fetch_array(mysql_query("select * from `city` where `city_id` = '$deposit_row[city_id]' limit 1"));
if ($_SESSION['city'] != $deposit_row['city_id']) {
	to("/$city_row[path]/deposits/$deposit_row[deposit_id]");
}

$head_title = "Вклад ".$deposit_row['name']." - «Банки России»";

require_once APP.HEADFILE;
?>
<script type="text/javascript" src="/files/js/bank_list.js"></script>
<h1>Вклад <?php echo $deposit_row['name']?></h1>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td align="left" valign="top" class="banki_main_col">
			<div id="credit_bank">Банк: <a href="<?php echo "/$c_name";?>/banks/<?php echo $deposit_row['link'].".html"; ?>"><?php
				echo $deposit_row['bank_name'];
			?></a></div><?php
			$dp_res = mysql_query("select * from `deposit_percent` where `deposit_id` = '$deposit_id' order by `dp_id`");
			$dp_rub = $dp_usd = $dp_eur = array();
			$i = $j = $k = 0;
			while ($dp_row = mysql_fetch_array($dp_res))
			{
				switch($dp_row['currency'])
				{
					case "RUB": $dp_rub[$i]['period'] = $dp_row['period'];
								$dp_rub[$i]['amount'] = $dp_row['amount'];
								$dp_rub[$i++]['percent'] = $dp_row['percent'];
								break;
					case "USD": $dp_usd[$j]['period'] = $dp_row['period'];
								$dp_usd[$j]['amount'] = $dp_row['amount'];
								$dp_usd[$j++]['percent'] = $dp_row['percent'];
								break;
					case "EUR": $dp_eur[$k]['period'] = $dp_row['period'];
								$dp_eur[$k]['amount'] = $dp_row['amount'];
								$dp_eur[$k++]['percent'] = $dp_row['percent'];
								break;
				}
			}
			
			if (($rub_count = count($dp_rub)) > 0)
			{
				$rub_period = $rub_amount = array();
				for ($i = 0; $i < $rub_count; $i++)
				{
					if (!in_array($dp_rub[$i]['period'],$rub_period))
						$rub_period[] = $dp_rub[$i]['period'];
					if (!in_array($dp_rub[$i]['amount'],$rub_amount))
						$rub_amount[] = $dp_rub[$i]['amount'];
				}
				$rub_period_count = count($rub_period);
				$rub_amount_count = count($rub_amount);
				?><table class='deposit_table' cellspacing="0" cellpadding="0">
					<tr>
						<td rowspan='2' class='deposit_currency'><strong>Рубли</strong></td>
						<td colspan='<?php echo $rub_period_count; ?>' style=" padding: 5px 100px;" class='deposit_period'><strong>Срок вклада</strong></td>
					</tr>
					<tr><?php
						for ($i = 0; $i < $rub_period_count; $i++)
							echo "<td>".getPeriod($rub_period[$i])."</td>";
					?></tr><?php
					for ($i = 0; $i < $rub_amount_count; $i++)
					{
						echo "<tr>";
						for ($j = 0; $j <= $rub_period_count; $j++)
						{
							if ($j == 0)
								echo "<td>от ".get_salary($rub_amount[$i])."</td>";
							else
								echo "<td>".number_format($dp_rub[$i*$rub_period_count+$j-1]['percent'],2)." %</td>";
						}
						echo "</tr>";
					}
				?></table>
				<div>&nbsp;</div><?php
			}
			if (($usd_count = count($dp_usd)) > 0)
			{
				$usd_period = $usd_amount = array();
				for ($i = 0; $i < $usd_count; $i++)
				{
					if (!in_array($dp_usd[$i]['period'],$usd_period))
						$usd_period[] = $dp_usd[$i]['period'];
					if (!in_array($dp_usd[$i]['amount'],$usd_amount))
						$usd_amount[] = $dp_usd[$i]['amount'];
				}
				$usd_period_count = count($usd_period);
				$usd_amount_count = count($usd_amount);
				?><table class='deposit_table' cellspacing="0" cellpadding="0">
					<tr>
						<td rowspan='2' class='deposit_currency'><strong>Доллары США</strong></td>
						<td colspan='<?php echo $usd_period_count; ?>' style=" padding: 5px 100px;" class='deposit_period'><strong>Срок вклада</strong></td>
					</tr>
					<tr><?php
						for ($i = 0; $i < $usd_period_count; $i++)
							echo "<td>".getPeriod($usd_period[$i])."</td>";
					?></tr><?php
					for ($i = 0; $i < $usd_amount_count; $i++)
					{
						echo "<tr>";
						for ($j = 0; $j <= $usd_period_count; $j++)
						{
							if ($j == 0)
								echo "<td>от ".get_salary($usd_amount[$i])."</td>";
							else
								echo "<td>".number_format($dp_usd[$i*$usd_period_count+$j-1]['percent'],2)." %</td>";
						}
						echo "</tr>";
					}
				?></table>
				<div>&nbsp;</div><?php
			}
			if (($eur_count = count($dp_eur)) > 0)
			{
				$eur_period = $eur_amount = array();
				for ($i = 0; $i < $eur_count; $i++)
				{
					if (!in_array($dp_eur[$i]['period'],$eur_period))
						$eur_period[] = $dp_eur[$i]['period'];
					if (!in_array($dp_eur[$i]['amount'],$eur_amount))
						$eur_amount[] = $dp_eur[$i]['amount'];
				}
				$eur_period_count = count($eur_period);
				$eur_amount_count = count($eur_amount);
				?><table class='deposit_table' cellspacing="0" cellpadding="0">
					<tr>
						<td rowspan='2' class='deposit_currency'><strong>Евро</strong></td>
						<td colspan='<?php echo $eur_period_count; ?>' style=" padding: 5px 100px;" class='deposit_period'><strong>Срок вклада</strong></td>
					</tr>
					<tr><?php
						for ($i = 0; $i < $eur_period_count; $i++)
							echo "<td>".getPeriod($eur_period[$i])."</td>";
					?></tr><?php
					for ($i = 0; $i < $eur_amount_count; $i++)
					{
						echo "<tr>";
						for ($j = 0; $j <= $eur_period_count; $j++)
						{
							if ($j == 0)
								echo "<td>от ".get_salary($eur_amount[$i])."</td>";
							else
								echo "<td>".number_format($dp_eur[$i*$eur_period_count+$j-1]['percent'],2)." %</td>";
						}
						echo "</tr>";
					}
				?></table><?php
			}
			?><div class="credit_second_table">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<?php
					if ($deposit_row['increase'] != "") {
						?><tr>
							<td width="47%" align="left" valign="top" class="credit_second_table_item">Пополнение</td>
							<td width="53%" align="left" valign="top" class="credit_second_table_item credit_second_table_item_grey"><?php
								echo $deposit_row['increase'] ? "Да" : "Нет";
							?></td>
						</tr><?php
					}
					if ($deposit_row['partial_taking'] != "") {
						?><tr>
							<td align="left" valign="top" class="credit_second_table_item">Частичное снятие</td>
							<td align="left" valign="top" class="credit_second_table_item credit_second_table_item_grey"><?php
								echo $deposit_row['partial_taking'] ? "Да" : "Нет";
							?></td>
						</tr><?php
					}
					if ($deposit_row['procent_charge'] != "")
					{
						?><tr>
							<td align="left" valign="top" class="credit_second_table_item">Начисление процентов</td>
							<td align="left" valign="top" class="credit_second_table_item credit_second_table_item_grey"><?php
								echo implode("<br />",explode(",",$deposit_row['procent_charge']));
							?></td>
						</tr><?php
					}
				?></table>
			</div>
		</td>
		<td width="159" align="left" valign="top"><?php
			require_once APP."rating_column.php";
			require_once APP."vacancy_column.php";
		?></td>
	</tr>
</table>
<div class="inside_block">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr valign='top'>
			<td width="344" class="inside_info"><?php
				require_once APP."bank_list.php";
				require_once APP."currency_rates.php";
			?></td>
			<td width="646"><?php
				$info = mysql_fetch_array(mysql_query("select * from `info` where `info_id` = '18' limit 1"));
				$text = getRichText($info['text']);
				echo "<h2>$info[header]</h2>";
				echo "<p>$text</p>";
			?></td>
		</tr>
	</table>
</div>
<?php require_once APP.FOOTERFILE; ?>